#encoding=utf-8
from utils.ext import inner_db
from marshmallow import Schema, fields, post_load
import time
from werkzeug.local import LocalProxy

current_time = LocalProxy(lambda: time.time()*1000)

class WorkOrderGet(Schema):
    uuid = fields.String(required=True)
    per_page_items = fields.Integer(required=True)
    page_index = fields.Integer(required=True)
    sort = fields.String(missing="default")

class WorkOrderAdd(Schema):
    uuid = fields.String(required=True)
    username = fields.String(required=True)
    kind = fields.Integer(required=True)
    priority = fields.Integer(required=True)
    tel = fields.String(required=True)
    email = fields.String(required=True)
    description = fields.String(required=True)
    create_time = fields.Integer(missing=int(current_time))

    @post_load
    def make_create_time(self, data, **kwargs):
        data['create_time'] = int(current_time)
        return data

class WorkOrderUpdate(Schema):
    id = fields.String(required=True)
    handler = fields.String(required=True)
    handle_result = fields.String(required=True)
    handle_time = fields.Integer(missing=int(current_time))
    status = fields.Integer(missing=1)

    @post_load
    def make_handle_time(self, data, **kwargs):
        data['handle_time'] = int(current_time)
        return data

class WorkOrderDelete(Schema):
    id = fields.String(required=True)
    uuid = fields.String(required=True)

class FaultKindAdd(Schema):
    name_cn = fields.String(required=True)
    name_en = fields.String()

class FaultKindUpdate(Schema):
    id = fields.Integer(required=True)
    name_cn = fields.String(required=True)
    name_en = fields.String()

class FaultKindDelete(Schema):
    id = fields.Integer(required=True)

class OrderBackendGetSchema(Schema):
    page_index = fields.Integer(missing=1)  ## 指定第几页，默认展示第一页
    per_page_items = fields.Integer(missing=5)  ## 每一页展示的行数

class OrderBackendUpdateSchema(Schema):
    id = fields.String(required=True)
    handle_result = fields.String()
    handler = fields.String(required=True)

class DownloadGetSchema(Schema):
    id = fields.String(required=True)
    uuid = fields.String(required=True)
    file_name = fields.String(required=True)

class FaultKind(inner_db.Model):
    __bind_key__ = 'management'
    __tablename__ = 'fault_kind'

    id = inner_db.Column(inner_db.Integer, primary_key=True, autoincrement=True, nullable=False)
    name_en = inner_db.Column(inner_db.String(100))
    name_cn = inner_db.Column(inner_db.String(100), nullable=False)


    def get_data_info(self):
        return dict(id = self.id,
                    name_en = self.name_en,
                    name_cn = self.name_cn,
                    )

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())


class WorkOrder(inner_db.Model):
    __bind_key__ = 'management'
    __tablename__ = 'worker_order'

    # 提交相关
    id = inner_db.Column(inner_db.String(50), primary_key=True, nullable=False)
    uuid = inner_db.Column(inner_db.String(100), nullable=False)
    username = inner_db.Column(inner_db.String(50), nullable=False)
    kind = inner_db.Column(inner_db.Integer, nullable=False)
    priority = inner_db.Column(inner_db.Integer, nullable=False)
    tel = inner_db.Column(inner_db.String(100), nullable=False)
    email = inner_db.Column(inner_db.String(100), nullable=False)
    description = inner_db.Column(inner_db.Text, nullable=False)
    attach = inner_db.Column(inner_db.String(100))
    create_time = inner_db.Column(inner_db.BigInteger)
    # 处理相关
    status = inner_db.Column(inner_db.Integer, default=0)
    handle_time = inner_db.Column(inner_db.BigInteger)
    handler = inner_db.Column(inner_db.String(50))
    handle_result = inner_db.Column(inner_db.Text)

    def get_data_info(self):
        return dict(id = self.id,
                    uuid = self.uuid,
                    username = self.username,
                    kind = self.kind,
                    priority = self.priority,
                    tel = self.tel,
                    email = self.email,
                    description = self.description,
                    attach = self.attach,
                    create_time = self.create_time,
                    status = self.status,
                    handle_time = self.handle_time,
                    handler = self.handler,
                    handle_result = self.handle_result
                    )

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())